package org.aplombh.java.leetcode.all;

public class _34在排序数组中查找元素的第一个和最后一个位置 {
    public static void main(String[] args) {
//        for (int i : new Solution34_1().searchRange(new int[]{2, 2}, 3)) {
//            System.out.print(i);
//        }
        System.out.println();
        for (int i : new Solution34_1().searchRange(new int[]{2,2}, 3)) {
            System.out.print(i);
        }
    }
}

class Solution34_1 {
    public int[] searchRange(int[] nums, int target) {

        if (nums.length == 0)
            return new int[]{-1, -1};

        int left = search(nums, target, 0, nums.length - 1);
        int right = search(nums, target + 1, 0, nums.length - 1) - 1;

        if (left != -1 && left < nums.length && nums[left] == target) {
            if (right == -1) right = 0;
            return new int[]{left, right};
        }

        return new int[]{-1, -1};

    }

    public int search(int[] nums, int target, int left, int right) {

        if (left > right)
            return left;

        int mid = (left + right) >> 1;

        if (target <= nums[mid])
            return search(nums, target, left, mid - 1);
        else
            return search(nums, target, mid + 1, right);
    }
}